package com.learn.core.controller.learn;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.learn.common.annotation.Log;
import com.learn.common.annotation.UserLoginToken;
import com.learn.common.enums.BusinessType;
import com.learn.common.enums.OperatorType;
import com.learn.common.enums.ResultEnum;
import com.learn.common.utils.JwtUtil;
import com.learn.common.utils.PageResult;
import com.learn.common.utils.ResultUtil;
import com.learn.common.utils.StringUtils;
import com.learn.common.utils.result.CommonResult;
import com.learn.core.domain.LMessage;
import com.learn.core.domain.LMessageUser;
import com.learn.core.domain.view.MessageUser;
import com.learn.core.dto.LMessageDTO;
import com.learn.core.service.ILMessageService;
import com.learn.core.service.ILMessageUserService;
import com.learn.core.service.IMessageUserService;
import com.learn.framework.web.controller.WebController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description
 *
 * @author zhaohongyu
 * @ClassName MessageController
 * @date 2021.06.22 09:33
 * @describe 消息
 */
@Api("消息")
@RestController
@RequestMapping("/learn/message")
public class MessageController extends WebController<LMessage> {

    @Autowired
    ILMessageService ilMessageService;
    @Autowired
    ILMessageUserService ilMessageUserService;
    @Autowired
    IMessageUserService iMessageUserService;

    //消息列表
    @ApiOperation(value = "消息列表")
    @UserLoginToken
    @PostMapping("/list")
    public PageResult<MessageUser> list(@RequestBody LMessageDTO lMessageDTO){
        try {
            if(lMessageDTO==null||lMessageDTO.getPage()==null||lMessageDTO.getRows()==null){
                return new PageResult<MessageUser>(null, null, ResultEnum.ERROR);
            }
            PageHelper.startPage(lMessageDTO.getPage(),lMessageDTO.getRows());
            QueryWrapper<MessageUser> queryWrapper = new QueryWrapper<MessageUser>();
            if (StringUtils.isNotEmpty(lMessageDTO.getState()))
                queryWrapper.eq("message_state",lMessageDTO.getState());
            if(StringUtils.isNotEmpty(lMessageDTO.getMessageType()))
                queryWrapper.eq("message_type",lMessageDTO.getMessageType());
            queryWrapper.orderByDesc("create_time");
            Page<MessageUser> lMessages = (Page<MessageUser>) iMessageUserService.list(queryWrapper);
            if (CollectionUtils.isEmpty(lMessages)) {
                return new PageResult<>(null, null, ResultEnum.NOT_EEIST);
            }
            // 返回
            return new PageResult(lMessages.getTotal(), lMessages, ResultEnum.SUCCESS);
        }catch (Exception e){
            return new PageResult(null,null,ResultEnum.ERROR);
        }
    }

    // 新消息数量
    @ApiOperation(value = "新消息数量")
    @PostMapping("/count")
    @UserLoginToken
    public CommonResult<Object> count(HttpServletRequest request) {
        String  token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        Map<String,Object> map = new HashMap<String,Object>();
        QueryWrapper<MessageUser> queryWrapper = new QueryWrapper<MessageUser>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("state","1");
        queryWrapper.eq("message_type","1");
        int sysCount = iMessageUserService.count(queryWrapper);
        map.put("sysCount",sysCount);
        queryWrapper = new QueryWrapper<MessageUser>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("message_type","1");
        queryWrapper.orderByDesc("create_time");
        MessageUser sysMessage = iMessageUserService.getOne(queryWrapper);
        if(null != sysMessage)
            map.put("sysMessage",sysMessage);
        else
            map.put("sysMessage",null);
        queryWrapper = new QueryWrapper<MessageUser>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("state","1");
        queryWrapper.eq("message_type","2");
        int impCount = iMessageUserService.count(queryWrapper);
        map.put("impCount",impCount);
        queryWrapper = new QueryWrapper<MessageUser>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("message_type","2");
        queryWrapper.orderByDesc("create_time");
        MessageUser impMessage = iMessageUserService.getOne(queryWrapper);
        if(null != impMessage)
            map.put("impMessage",impMessage);
        else
            map.put("impMessage", null);
        queryWrapper = new QueryWrapper<MessageUser>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("state","1");
        queryWrapper.eq("message_type","3");
        int examCount = iMessageUserService.count(queryWrapper);
        map.put("examCount",examCount);
        queryWrapper = new QueryWrapper<MessageUser>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("message_type","3");
        queryWrapper.orderByDesc("create_time");
        MessageUser examMessage = iMessageUserService.getOne(queryWrapper);
        if(null != examMessage)
            map.put("examMessage",examMessage);
        else
            map.put("examMessage",null);
        queryWrapper = new QueryWrapper<MessageUser>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("state","1");
        queryWrapper.eq("message_type","4");
        int comCount = iMessageUserService.count(queryWrapper);
        map.put("comCount",comCount);
        queryWrapper = new QueryWrapper<MessageUser>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("message_type","4");
        queryWrapper.orderByDesc("create_time");
        MessageUser comMessage = iMessageUserService.getOne(queryWrapper);
        if(null != comMessage)
            map.put("comMessage",comMessage);
        else
            map.put("comMessage",null);
        return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS,map);
    }

    //更新
    @ApiOperation(value = "更新")
    @PostMapping("/edit")
    @UserLoginToken
    @Log(title = "更新消息信息", businessType = BusinessType.UPDATE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> edit(@RequestBody LMessageDTO lMessageDTO,HttpServletRequest request) {
        String  token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        QueryWrapper<MessageUser> queryWrapper = new QueryWrapper<MessageUser>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("state","1");
        if(lMessageDTO.getMessageType()!=null)
            queryWrapper.eq("message_type",lMessageDTO.getMessageType());
        List<MessageUser> list = iMessageUserService.list(queryWrapper);
        for (MessageUser messageUser: list) {
            LMessageUser lMessageUser = new LMessageUser();
            lMessageUser.setMessageUserId(messageUser.getMessageUserId());
            lMessageUser.setState("2");
            ilMessageUserService.updateById(lMessageUser);
        }
        return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
    }

    //详情
    @ApiOperation(value = "详情")
    @PostMapping("/info")
    @UserLoginToken
    public CommonResult<Object> info(@RequestBody LMessageDTO lMessageDTO) {
        LMessage lMessage = ilMessageService.getById(lMessageDTO.getId());
        return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS,lMessage);
    }
}
